package cc.mrbird.febs.system.service.impl;

import cc.mrbird.febs.common.entity.PayConstant;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.system.entity.SystemParam;
import cc.mrbird.febs.system.mapper.SystemParamMapper;
import cc.mrbird.febs.system.service.ISystemParamService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * 系统参数表 Service实现
 *
 * @author Mr.Yang
 * @date 2022-08-10 23:38:46
 */
@Slf4j
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class SystemParamServiceImpl extends ServiceImpl<SystemParamMapper, SystemParam> implements ISystemParamService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private SystemParamMapper systemParamMapper;

    @Override
    public IPage<SystemParam> findSystemParams(QueryRequest request, SystemParam systemParam) {
        LambdaQueryWrapper<SystemParam> queryWrapper = new LambdaQueryWrapper<>();
        // TODO 设置查询条件
        Page<SystemParam> page = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(page, queryWrapper);
    }

    @Override
    public List<SystemParam> findSystemParams(SystemParam systemParam) {
        LambdaQueryWrapper<SystemParam> queryWrapper = new LambdaQueryWrapper<>();
        // TODO 设置查询条件
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional
    public void createSystemParam(SystemParam systemParam) {
        this.save(systemParam);
    }

    @Override
    @Transactional
    public void updateSystemParam(SystemParam systemParam) {
        this.saveOrUpdate(systemParam);
    }

    @Override
    @Transactional
    public void deleteSystemParam(SystemParam systemParam) {
        LambdaQueryWrapper<SystemParam> wrapper = new LambdaQueryWrapper<>();
        // TODO 设置删除条件
        this.remove(wrapper);
    }


    @Override
    public void initSystemParam2Redis() {
        List<SystemParam> systemParams = systemParamMapper.selectList(new LambdaQueryWrapper<>());
        Set<String> keys = stringRedisTemplate.keys(PayConstant.SUFFIX + "*");
        stringRedisTemplate.delete(keys);
        for (SystemParam systemParam : systemParams) {
            stringRedisTemplate.opsForValue().set(PayConstant.SUFFIX + systemParam.getParamKey(), systemParam.getParamValue());
            log.info("初始化系统参数：{}", systemParam.getParamName());
        }


    }
}
